﻿using AutoBlogger.DB;
using AutoBlogger.Properties;
using AutoBlogger.Tool;
using System;
using System.IO;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace AutoBlogger.View
{
    public partial class MainForm : Form
    {
        internal static bool IsClose { get; private set; }
        
        public static bool IsGo { get; set; }
        string lockVar = string.Empty;

        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            //加入热键
            //HotKeyControl.RegisterHotKey(this.Handle, HotKeyControl.HotKeyId, HotKeyControl.ControlKey, HotKeyControl.HotKey);

            //NetClient.PostBlog(new Blog() { Title = "test1", Body = "test2" });
            //translateBlogs(null);

            //Settings.Default.CurrentNum = 229;
            //Settings.Default.Save();
            IsGo = true;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //this.timer1.Enabled = true;
            //Proxies proxies = new Proxies();

            //foreach (var proxy in proxies.CanUsedProxies)
            //{
            //    ThreadPool.QueueUserWorkItem(new WaitCallback(GetBlog), proxy);
            //}
            ThreadPool.QueueUserWorkItem(new WaitCallback(GetBlog), string.Empty);
            //GetBlog(null);
        }

        private void GetBlog(object state)
        {
            string baseUrl = "http://fanyi.youdao.com/WebpageTranslate?keyfrom=fanyi.web.index&url=http://home.meishichina.com/recipe-{0}.html&type=AUTO";
            uint c = Settings.Default.CurrentNum;
            //this.webBrowser1.Url = new Uri();
            lock (lockVar)
            {
                Settings.Default.CurrentNum++;
            }
            YouDao youDao = new YouDao(string.Format(baseUrl, c));
            saveBlog(youDao.TranslatedResult);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //this.timer1.Enabled = false;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Settings.Default.Reset();
        }

        private void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e)
        {

        }

        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            //            if (this.webBrowser1.ReadyState < WebBrowserReadyState.Complete || (this.webBrowser1.Url.AbsoluteUri == lastUrl))
            //            {
            //                return;
            //            }
            //            var content = this.webBrowser1.Document.GetElementById("Main");
            //            if (content == null)
            //            {
            //                return;
            //            }

            //            if (this.webBrowser1.Url.Host == "fanyi.youdao.com")
            //            {
            //                NetClient.GetBlogEntity(webBrowser1.Document.ToString());
            //                BlogModel model = NetClient.GetBlogEntity(string.Empty);
            //                this.webBrowser1.Stop();
            //                if (string.IsNullOrEmpty(lastUrl) || e.Url.AbsoluteUri == lastUrl)
            //                {
            //                    lock (lockVar)
            //                    {
            //                        if (!DB.MongoDbHelper.IsExisting(Settings.Default.CurrentNum))
            //                        {
            //                            lastUrl = this.webBrowser1.Url.AbsoluteUri;
            //                            webBrowser1.ReadyState==WebBrowserReadyState.Complete
            //                            saveBlog();
            //                        }
            //                    }
            //                }
            //            }
            //            else if (e.Url.AbsoluteUri.StartsWith("http://dog.youdao.com/redirect"))//跳转
            //            {
            //                this.webBrowser1.Navigate(@"javascript:
            //            var lnks = document.getElementsByTagName('a');
            //            for (var i = 0; i < lnks.length; i++) {
            //                var url = lnks.item(i).attributes.getNamedItem('href').nodeValue;
            //                if (url.substring(0, 12) == 'http://fanyi') {
            //                    lnks.item(i).click();
            //                } 
            //            }");

            //            }
            //            else if (e.Url.Host == "dog.youdao.com")
            //            {

            //            }
        }

        private void saveBlog(string documentText)
        {
            DB.MongoDbHelper.AddBlog(new BlogModel(documentText));

            this.label1.Invoke(new Action(() =>
            {
                this.label1.Text = Settings.Default.CurrentNum.ToString();
            }));

            //this.label1.Text = Settings.Default.CurrentNum.ToString();
            Settings.Default.Save();
            Thread.Sleep(10000);
            GetBlog(null);
        }

        private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
        {
            //if (e.Url.Query.StartsWith("?type=ZH_CN2EN"))
            //{
            //    e.Cancel = true;
            //}
            //string[] ps = e.Url.Query.Remove(0, 1).Split(new char[] { '&' });

            //foreach (var q in ps)
            //{
            //    if (q.StartsWith("type="))
            //    {
            //        e.Cancel = true;
            //        break;
            //    }
            //}
            //var endStr = e.Url.AbsoluteUri.Substring(e.Url.AbsoluteUri.Length - 3, 3).ToLower();
            //switch (endStr)
            //{
            //    case "gif":
            //    case "png":
            //    case "jpg":
            //        e.Cancel = true;
            //        break;
            //    default:
            //        break;
            //}
        }

        private void button4_Click_1(object sender, EventArgs e)
        {
            MongoDbHelper.PublishBlog(this.label2);
        }

        private void button5_Click(object sender, EventArgs e)
        {
            IsGo = false;
        }
    }
}
